package web_view_controller

import (
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
	"strconv"
	"transport/framework/fw_db/fw_db_self"
	"transport/framework/fw_middleware"
	"transport/framework/fw_utils"
	"transport/web/web_view_mapper"
)

type orderHeaderReportController struct {
}

func (controller *orderHeaderReportController) Page(c *gin.Context) {
	var err error
	var from, limit = 1, 10
	if tmp := c.Request.FormValue("pageSize"); tmp != "" {
		limit, _ = strconv.Atoi(tmp)
	}
	if tmp := c.Request.FormValue("pageIndex"); tmp != "" {
		from, _ = strconv.Atoi(tmp)
	}
	orderCode := c.Request.FormValue("orderCode")
	billSn := c.Request.FormValue("billSn")
	customerName := c.Request.FormValue("customerName")
	trusteeName := c.Request.FormValue("trusteeName")
	routeName := c.Request.FormValue("routeName")
	priorityLevel := c.Request.FormValue("priorityLevel")
	status := c.Request.FormValue("status")
	mapper := web_view_mapper.OrderHeaderReportMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	items, total, err := mapper.Page(from, limit, orderCode, billSn, customerName, routeName, priorityLevel, status, trusteeName)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.PageOK(c, items, total, from, limit, "ok")
}

func (controller *orderHeaderReportController) List(c *gin.Context) {
	var err error
	mapper := web_view_mapper.OrderHeaderReportMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	orderCode := c.Request.FormValue("orderCode")
	billSn := c.Request.FormValue("billSn")
	customerName := c.Request.FormValue("customerName")
	trusteeName := c.Request.FormValue("trusteeName")
	routeName := c.Request.FormValue("routeName")
	priorityLevel := c.Request.FormValue("priorityLevel")
	status := c.Request.FormValue("status")
	items, total, err := mapper.List(orderCode, billSn, customerName, routeName, priorityLevel, status, trusteeName)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	fw_utils.ListOK(c, items, total, "ok")
}
func (controller *orderHeaderReportController) ExportExcel(c *gin.Context) {
	var err error
	mapper := web_view_mapper.OrderHeaderReportMapper{
		DB:   fw_db_self.GetSqliteDB(),
		Ctx:  c,
		Lock: fw_db_self.GetSqliteLock(),
	}
	orderCode := c.Request.FormValue("orderCode")
	billSn := c.Request.FormValue("billSn")
	customerName := c.Request.FormValue("customerName")
	trusteeName := c.Request.FormValue("trusteeName")
	routeName := c.Request.FormValue("routeName")
	priorityLevel := c.Request.FormValue("priorityLevel")
	status := c.Request.FormValue("status")
	items, _, err := mapper.List(orderCode, billSn, customerName, routeName, priorityLevel, status, trusteeName)
	if err != nil {
		fw_utils.Error(c, -1, err, "")
		return
	}
	f := excelize.NewFile()
	// header
	colRowHeader, _ := excelize.CoordinatesToCellName(1, 1)
	f.SetSheetRow("Sheet1", colRowHeader, &[]interface{}{
		"订单号", "运单号", "客户名称", "委托商", "承运商", "车牌号", "司机", "线路", "箱/包数"})
	//填充数据
	for k, v := range items {
		colRowItems, _ := excelize.CoordinatesToCellName(1, k+2) //  从第一列 第二行开始
		f.SetSheetRow("Sheet1", colRowItems, &[]interface{}{v.OrderCode, v.TransBillSn, v.CustomerName,
			v.TrusteeName, v.CarrierName, v.CarSn, v.DriverName, v.Route, v.PackCount})
	}
	//if err := f.SaveAs("Book1.xlsx"); err != nil {
	//	fw_log.Slogger.Info(err.Error())
	//}
	c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
	c.Header("Content-Disposition", "attachment; filename=订单头报表.xlsx")
	c.Header("Cache-Control", "no-cache")
	//设置文件名称
	//c.Header("response-type", "blob") // 以流的形式下载必须设置这一项，否则前端下载下来的文件会出现格式不正确或已损坏的问题
	f.Write(c.Writer)
}
func OrderHeaderReportRoute(r *gin.Engine) {
	controller := orderHeaderReportController{}
	v1 := r.Group("/trans").Use(fw_middleware.JwtAuthMiddleware())
	{
		v1.GET("/v1/orderHeaderReport/page", controller.Page)
		v1.GET("/v1/orderHeaderReport/list", controller.List)
		v1.GET("/v1/orderHeaderReport/exportExcel", controller.ExportExcel)
	}
}
